{$MODE OBJFPC}

uses
  // Free Pascal units
  Windows, SysUtils,
  // S2DE units
  S2DMain, S2DLog, S2DTypes, S2DSWCanvas;

var P1, P2, P3, Fade: TBuffer;
    Val, IncVal, Sw : Byte;

begin
  // nap resource & khoi tao window
  InitBuffer(Fade, 800, 600);
  LoadBMP(P1, 'Images\11.bmp');
  LoadBMP(P2, 'Images\12.bmp');
  LoadBMP(P3, 'Images\13.bmp');
  InitWindow(800, 600);
  ShowCursor(FALSE);
  Val:= 252;
  IncVal:= -4;
  Sw:= 1;
  // main loop
  repeat
    Inc(Val, IncVal);
    if (Val = 0) then
    begin
      Val:= 252;
      Inc(Sw);
      if Sw = 4 then
        Sw:= 1;
    end;
    // swap 2 buffer P1 & P2
    if Sw = 1 then
    begin
      SwapBuffer(Screen, P1);
      SwapBuffer(Fade, P2);
      SubBar(Screen, 0, 0, 1365, 767, RGBToDWord(255-Val, 255-Val, 255-Val));
      SubBar(Fade, 0, 0, 1365, 767, RGBToDWord(Val, Val, Val));
    end;
    // swap 2 buffer P2 & P3
    if Sw = 2 then
    begin
      SwapBuffer(Screen, P2);
      SwapBuffer(Fade, P3);
      SubBar(Screen, 0, 0, 1365, 767, RGBToDWord(255-Val, 255-Val, 255-Val));
      SubBar(Fade, 0, 0, 1365, 767, RGBToDWord(Val, Val, Val));
    end;
    // swap 2 buffer P3 & P1
    if Sw = 3 then
    begin
      SwapBuffer(Screen, P3);
      SwapBuffer(Fade, P1);
      SubBar(Screen, 0, 0, 1365, 767, RGBToDWord(255-Val, 255-Val, 255-Val));
      SubBar(Fade, 0, 0, 1365, 767, RGBToDWord(Val, Val, Val));
    end;
    AddBuffer(Screen, Fade, 0, 0);

    GetMessages;
  until KeyMap[256] OR NOT S2DInfo.Running;
  // giai phong buffer & thoat chuong trinh
  FreeBuffer(P3);
  FreeBuffer(P2);
  FreeBuffer(P1);
  FreeBuffer(Fade);
  CloseWindow;
end.

